import { message } from 'ant-design-vue'
import store from '@/store'
import Vue from 'vue'
import {
    getHotelsAPI,
    getHotelByIdAPI,
    getUserCommentAPI,
    submitHotelSearchParamsAPI,
    mockgetUserCommentAPI,
    gethotelpicAPI
} from '@/api/hotel'
import {
    reserveHotelAPI
} from '@/api/order'
import {
    orderMatchCouponsAPI,
} from '@/api/coupon'
import {
    bindManagerAPI
} from "@/api/admin";

const hotel = {
    state: {

        hotelList: [

        ],
        hotelListParams: {
            pageNo: 0,
            pageSize: 12
        },
        hotelListLoading: true,
        currentHotelId: '',
        currentHotelInfo: {

        },

        orderModalVisible: false,
        currentOrderRoom: {

        },
        orderMatchCouponList: [
        ],
        //下面三个是酒店列表使用的
        searchedHotelList:[
        ],
        userComment:[
        ],

      //暂且先扔到这里，设置酒店位置也要用
        // //添加用户评论！！！
        mockuserComment:[],
        hotelpic:{}
    },
    mutations: {
        set_hotelList: function(state, data) {
            state.hotelList = data
        },
        set_hotelListParams: function(state, data) {
            state.hotelListParams = {
                ...state.hotelListParams,
                ...data,
            }
        },
        set_hotelListLoading: function(state, data) {
            state.hotelListLoading = data
        },
        set_currentHotelId: function(state, data) {
            state.currentHotelId = data
        },
        set_currentHotelInfo: function(state, data) {
            state.currentHotelInfo = {
                ...state.currentHotelInfo,
                ...data,
            }
        },
        set_orderModalVisible: function(state, data) {
            state.orderModalVisible = data
        },
        set_currentOrderRoom: function(state, data) {
            state.currentOrderRoom = {
                ...state.currentOrderRoom,
                ...data,
            }
        },
        set_orderMatchCouponList: function(state, data) {
            state.orderMatchCouponList = data
        },
        //搜索酒店后的设置
        set_searchedHotelList:function (state , data) {
            // state.searchedHotelList=data
            console.log("HHHHHH", data)
            Vue.set(state, 'searchedHotelList', data);
        },
    //    添加用户评论
        set_userComment: function (state,data) {
            state.userComment=data;
        },
        set_mockuserComment: function (state,data) {
            state.mockuserComment=data;
        },
        set_hotelpic:(state, data) => {
            state.hotelpic = data
        },
    },

    actions: {
        getHotelList: async({commit, state}) => {
            const res = await getHotelsAPI()
            if(res){
                var  key='rate';
                res.sort(function (a, b) {
                    let x = a[key]
                    let y = b[key]
                    return ((x > y) ? -1 : (x > y) ? 1 : 0)
                })
                commit('set_hotelList', res)
                commit('set_hotelListLoading', false)
            }
        },
        getHotelById: async({commit, state}) => {
            const res = await getHotelByIdAPI({
                hotelId: state.currentHotelId
            })
            if(res){
                commit('set_currentHotelInfo', res)
            }
        },
        // 添加用户评论
        getUserComment: async({commit, state},id) => {
            // console.log('发出了请求')
            const res = await getUserCommentAPI(id);
            // console.log(res)
            if(res){
                commit('set_userComment', res)
            }
        },

        addOrder: async({ state, commit }, data) => {
            const res = await reserveHotelAPI(data)
            console.log('返回订单信息')
            console.log(res)
            if(res){
                message.success('预定成功')
                commit('set_orderModalVisible', false)
            }
        },
        getOrderMatchCoupons: async({ state, commit }, data) => {
            const res = await orderMatchCouponsAPI(data)
            if(res){
                commit('set_orderMatchCouponList', res)
            }
        },
        submitHotelSearchParams:async({ state, commit }, data) =>{
            const res=await submitHotelSearchParamsAPI(data)
            if(res){
                console.log('nadaol')
                console.log(res)
                commit('set_searchedHotelList',res)
            }else {
                console.log("搜索失败")
            //    错误信息以后再说
            }
        },

        // bindManager: async({ state, commit }, data) => {
        //     const res = await orderMatchCouponsAPI(data)
        //     if(res){
        //         commit('', res)
        //     }
        // }
        mockgetUserComment: async({commit, state}) => {
            const res = await mockgetUserCommentAPI();
            if(res){
                commit('set_mockuserComment', res)
            }
        },
        bindManager: async({ state, dispatch }, data) => {
            const res = await bindManagerAPI(data)
            message.success('绑定成功')
            dispatch('getHotelList')
        },
        gethotelpic: async({ state, commit}, data) => {
            const res = await gethotelpicAPI(data)
            if (res) {
                commit('set_hotelpic', res)
            }
            else {
                console.log("获取酒店图片失败")
                console.log(res)
                var pic={
                    pic:null
                }
                commit('set_hotelpic', pic)
            }
        },

    }
}

export default hotel
